from Foo.capability_json import getDriver
from Foo.login import Login
from Foo.pageSwitch import PageSwitch
from util.baseView import BaseView
from selenium.webdriver.common.by import By
from appium.webdriver.common.touch_action import TouchAction
import logging
import logging.config
from faker import Faker

import time

'''
desc：HD091校园试点,单机人员新增和采集功能
auth：shiwen
date:2020-12-09
'''

class Person(BaseView):
    CON_LOG = '../docs/log.conf'
    logging.config.fileConfig(CON_LOG)
    f = Faker(locale='zh_CN')
    count = 0

    def __init__(self, driver):
        BaseView.__init__(self, driver)
        self.image_icon = (By.ID, 'com.sykean.biometrics:id/imageView_face_icon')
        self.edit = (By.ID, 'com.sykean.biometrics:id/modify')  # 人员编辑按钮
        self.iris_capture = (By.ID, 'com.sykean.biometrics:id/btn_iris_capture')  # 虹膜采集
        self.face_capture = (By.ID, 'com.sykean.biometrics:id/btn_face_capture')
        self.dialog_ok = (By.ID, 'com.sykean.biometrics:id/btn_dialog_ok')  # 确认是否重新采集虹膜
        self.dialog_cancel = (By.ID, 'com.sykean.biometrics:id/btn_dialog_cancel')  # 确认是否取消本次操作
        self.back = (By.ID, 'com.sykean.biometrics:id/btn_back')
        self.add_user = (By.ID,'com.sykean.biometrics:id/ibtn_add_user')
        # self.edit_values = (By.ID,'com.sykean.biometrics:id/edt_edit_value')
        self.save_user = (By.ID,'com.sykean.biometrics:id/btn_save_user')
    def test_edit_person(self):
        el = self.find_element(*self.image_icon)
        # 长按
        TouchAction(self.driver).long_press(el).perform()
        time.sleep(2)
        self.click_element(*self.edit)
    def test_add_user(self,count):

        # 人员新增按钮
        self.click_element(*self.add_user)

        eles = self.driver.find_elements_by_class_name("android.widget.EditText")
        # 编号
        eles[0].send_keys(self.f.random_int())
        # 姓名
        eles[1].send_keys(self.f.name())
        # 身份证号
        eles[2].send_keys(self.f.ssn())
        # 卡号
        # eles[3].send_keys(self.f.phone_number())
        # 部门
        eles[3].send_keys(self.f.company_prefix())
        # 韦根
        eles[5].send_keys(self.f.phone_number())
        # 传参count,需要使用全局变量count
        self.test_regist(self.count)

        self.click_element(*self.save_user)
        logging.info('---人员新增成功---')

    def test_regist(self,count):
        # 特征采集
        try:
            self.click_element(*self.iris_capture)
            logging.info('--第{}次采集开始---'.format(count))
            print('全局count',count)
            self.count += 1
            time.sleep(5)
            self.click_element(*self.back)
        except Exception as e:
            logging.info('---生物特征采集异常---')

    def test_back(self):
        self.click_element(*self.back)


if __name__ == '__main__':
    driver = getDriver()
    Login(driver).login()
    PageSwitch(driver).menuUser()

    p = Person(driver)
    while True:

        try:
            p.test_add_user(p.count)

        except:
            print('---异常终止，请继续---')
            Person(driver).test_back()





